import axios from 'axios';
// import { Notification } from 'element-ui';
// import Storage from '@/config/storage';
import VueRouter from 'vue-router';
import { baseUrl } from '@/config/env';
import store from '@/store/index.js'

const router = new VueRouter();

let config = {
	timeout: 8* 1000,
	baseURL: baseUrl + '/admin/',
	validateStatus: function (status) {
		return status >= 200 && status < 500; // 默认<300
	},
};

const http = axios.create(config);

http.interceptors.request.use(config => {
	// const session = Storage.getSession();
	const session = store.state.token;
	
	if (session) {
		config.headers.Authorization = `Bearer ${session}`;
		// config.url += '?api_token='+session;
	}
	return config;

}, err => Promise.reject(err));

/**
 * sd-erp api
 */
http.interceptors.response.use(response => {


	const { data } = response;
	switch(data.res) {
		case 1:
			return Promise.resolve(data);
		case 0:
			// Notification.error({
			// 	message: data.msg,
			// 	duration: 5000
			// });
			alert(data.msg);
			return Promise.reject(data);
			case -1:
				router.push({path: '/login'});
				location.reload();
				break;
		default:
			// Notification.error({
			// 	message: '其他异常！',
			// 	duration: 5000
			// });
			alert('其他异常');
			return Promise.reject(data);  
    }
}, err => {
	// Notification.error({
	// 	message: '服务器异常！',
	// 	duration: 5000
	// });
	alert('服务器异常！');
	return Promise.reject(err);  
	
});


export default http;